<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_fetcher extends CI_Model {
	
	
	function fetch_table($table,$order=null,$limit=null,$where=null)
	{				
		
		if($order!=null)
			$this->db->order_by($order[0],$order[1]);
		elseif($table == 'tb_mas_content')
			$this->db->order_by('dteStart','desc');
		
		if($limit!=null)
			$this->db->limit($limit);
			
		if($where!=null)
			$this->db->where($where);
		
		$data =  $this->db
						->get($table)
						->result_array();
						
		return $data;
						
	}
    function getCode($code)
    {
        $code = $this->db->get_where('tb_mas_codes',array('strCode'=>$code))->result_array();
        if(!empty($code))
        {
            $code = current($code);
            return $code;
        }
        else
            return false;
    }
    function viewContent($id)
    {
        $item = current($this->db->get_where('tb_mas_content',array('intID'=>$id))->result_array());
        $data['intViews'] = $item['intViews']+1;
        $this->db->where('intID',$id)
            ->update('tb_mas_content',$data);
        return $item;
    }
    function getFeaturedContent($limit)
    {
        $this->db
             ->select('*')
             ->from('tb_mas_content');
        
        $this->db
             ->where('intFeatured >',0)
             ->limit($limit)
             ->order_by('intFeatured','asc');
        
        return $this->db
                    ->get()
                    ->result_array();
    }
    function getContent($category,$limit=5,$tree=false,$trending=false)
    {
        $cat = current($this->db->get_where('tb_mas_category',array('strName'=>$category))->result_array());
        
        
        $this->db
             ->select('*')
             ->from('tb_mas_content');
        if($trending)
            $this->db->order_by('intViews','desc');
        else
            $this->db->order_by('dteStart','desc');
             
       $arr =$this->db->limit($limit)
             ->get()
             ->result_array();
        
        $ret = array();
        
        if($tree)
        {
            $s_array = $this->getSuccessors($cat);
            foreach($arr as $a)
            {
                $cats = explode(';',$a['intContentCategory']);
                foreach($cats as $c)
                {
                    if(in_array($c,$s_array))
                    {
                        $ret[] = $a;
                        break;
                    }
                }
                    
            }
        }
        else
        {
            foreach($arr as $a)
            {
                $cats = explode(';',$a['intContentCategory']);
                foreach($cats as $c)
                {
                    if($c==$cat['intID'])
                    {
                        $ret[] = $a;
                        break;
                    }
                }
                    
            }
        }
        //print_r($ret);
        return $ret;
    }
    
    
    function getPredecessors($cat)
    {
        $c = $cat['intParent'];
        $retval[] = $cat['intID'];
        while($c!=0){
            $retval[] = $cat['intParent'];
            $cat = current($this->db->get_where('tb_mas_category',array('intID'=>$c))->result_array());
            $c = isset($cat['intParent'])?$cat['intParent']:0;
        }
        
        return $retval;
    }
    
    function getSuccessors($cat)
    {

        $c = $cat['intID'];
        $r[] = $c;
        $succ = $this->db->get_where('tb_mas_category',array('intParent'=>$c))->result_array();
        if(!empty($succ))
            foreach($succ as $s)
                $r = array_merge($r,$this->getSuccessors($s));
           
        return $r;    
    }
  
    

 
    
    
    
    

}